import Parser from "web-tree-sitter";
import * as vscode from "vscode";

export interface FunctionInfo {
  code: string | undefined;
  returnTypeName: string | undefined;
  sourceClassName: string | undefined;
  sourceFuncName: string | undefined;
  paramTypeName: string[];
  paramName: string[];
  memberVariables: Set<string>;
  identifiers: Set<string>;
  signature: string;
  access?: string;
  paramList?: string[];
  other_virtual_function_signature?: string[];
  sourceFunc?: NameAndPos | undefined;
}

export interface CommonCppPromptContext {
  language: string;
  ide: string;
  start_row?: number; // 评测用
  end_row?: number; // 评测用
  code: string;
  origin_code: string;
  cls: string;
  param_type_and_definition: Definition[];
  source_method_class: string;
  source_class_fields: string[] | string;
  source_file_global_fields: string[];
  source_method_file_path: string;
  source_method_signature: string;
  source_method_name: string | undefined;
  source_class_type_constructors_map: { [key: string]: string[] };
  callees_map: { [key: string]: CalleeContext[] };
  source_class_singleton_methods: string[];
}

export interface CppPromptContext extends CommonCppPromptContext {
  source_file_include_info: string[];
  source_method_signature_with_comment: string;
  source_file_namespace_list: string[];
  source_method_branch_instruct?: string | string[]; // 分支增强用
  source_method_branch_map?: any[]; // 分支增强用
  ut_scene?: string;
}

export interface BaseCommonCppContext{
  language: string;
  ide: string;
  start_row?: number; // 评测用
  end_row?: number; // 评测用
  code: string;
  origin_code: string;
  cls: string;
  source_method_file_path?: string;
  source_method_signature: string;
  source_method_name: string | undefined;
}

export interface HarmonyCppPromptContext extends BaseCommonCppContext {
  is_static_method: Boolean;
  is_template_method: Boolean;
  callee_method_class: { [key: string]: string[] };
  init_head_macro: string;
  returnType: string,
  sourceClass: string,
  sourceClassConstructors: { [key: string]: string[]; },
  sourceClassMembers: string[],
  paramDefinitions: string[],
  signature: string,
  targetFunctionCode: string,
  addRowFunctionCode: string,
  modulePath: string,
  sourceMethodBranchMap: { key: string, value: string }[],
  is_harmony_scenario: Boolean,
  gmock_template_str: string,
  cpp_stub_template_str: string,
  source_cls_namespace_name: string,
  existing_test_case: string[],
  existing_test_suite: string,
  existing_gmock_def: { key: string, value: string }[],
  existing_cpp_stub_def: { key: string, value: string[] }[],
  interface_definitions: string[],
  funcCalledList: FuncCalledInfo[],
  source_class_constructor_method: string,
  source_class_name_lower: string,
  gmock_str: string,
  cppstub_str: string,
  include_statement: string,
  using_namespace: string,
  other_str: string,
  source_class_fields: string
}

export interface FuncCalledInfo {
  sourceClassName: string | undefined;
  sourceFuncName: string;
  declarator: string,
  isGlobleFun: boolean,
  isVirtual: boolean,
  isStatic: boolean,
  locationRow: number,
  virtualFunction: string,
  signature: string,
}

export interface PathMap {
  key: string,
  value: string,
  lastRow?: number,
  gmockTemplateStrList?: string[],
  cppStubTemplateStrList?: string[],
  gmock_template_str?: string,
  cpp_stub_template_str?: string
}

export interface Node {
  node: any; // Parser.SyntaxNode
  hasReturn: boolean;
  elseReturn: boolean;
  conditionText: string | null;
  parentCondition: Node | null;
  branchTestText: string;
  text: string;
  type: string;
  associatedParent: {
    isElse: boolean;
    isParent: boolean;
  };
  id: string | number;
}

export interface CalleeContext {
  name: string;
  signature: string;
  access: string;
  cls: string;
  is_src_cls_member: Boolean;
  mock_type: string;
  mock_statement?: string;
  other_virtual_function_signature?: string[];
}

export interface Definition {
  type: string;
  name: string;
  definition: string;
}

export interface DefinitionInfo {
  range: vscode.Range;
  type: string;
  name: string;
  definition?: string; // 完整的定义文本
  document: vscode.TextDocument;
  node?: any; // Parser.SyntaxNode
}

// 定义所有语言共有的定义类型
export type BaseDefinitionType = 'variable' | 'function' | 'class' | 'interface' | 'enum' | 'type' | 'typedef';

// C++ 特有的定义类型
export type CppSpecificDefinitionType = 'macro' | 'struct' | 'union' | 'enumerator';

// 定义一个通用的 DefinitionType，包含所有语言的定义类型
export type DefinitionType = BaseDefinitionType | CppSpecificDefinitionType;

export interface SnippetDefinition<T extends DefinitionType = DefinitionType> {
  name: string;
  location: vscode.Location;
  text: string;
  refPosition: vscode.Position[];
  type: T;
  provider?: 'Compiler' | 'Tag';
}

export interface TextSnippetDefinitionsRequest<T extends DefinitionType = DefinitionType> {
  symbols: string[];
  allowedTypes?: T[];
  token?: vscode.CancellationToken;
}

export interface SnippetDefinitionsRequest<T extends DefinitionType = DefinitionType> {
  document: vscode.TextDocument;
  range: vscode.Range;
  allowedTypes?: T[];
  token?: vscode.CancellationToken;
}

export interface SnippetDefinitionsResponse<T extends DefinitionType = DefinitionType> {
  definitions: SnippetDefinition<T>[];
}

export interface Definitions {
  struct: SnippetDefinition[],
  macro: SnippetDefinition[],
  typedef: SnippetDefinition[],
  function: SnippetDefinition[],
  class: SnippetDefinition[],
  union: SnippetDefinition[],
  enum: SnippetDefinition[],
  enumerator: SnippetDefinition[],
  variable: SnippetDefinition[],
}
export interface NameAndPos {
  name: string;
  position: vscode.Range;
}

export interface TestCase {
  range: vscode.Range;
  text: string;
  testSuite: NameAndPos;
}

export interface DefsInFunc {
  return: string;
  class: string;
  classCtors: { [key: string]: string[]; };
  classMembers: string[];
  classNamespace: string;
  parameters: string[];
  others: { [key: string]: string[]; }
}

// 元素参数接口
export interface EleParameters {
  doc: vscode.TextDocument;
  pos: vscode.Position;
  cancellationToken?: vscode.CancellationToken;
  funcDefNeedFilterSymbols?: string[];
  timeoutMs?: number;
}

export interface CxxEleParameters extends EleParameters {
  cancellationToken?: vscode.CancellationToken;
  funcDefNeedFilterSymbols?: string[];
}

// 签名接口
export interface Signature {
  signature: string;
  funcRange: vscode.Range;
}

// Cxx元素接口
export interface CxxElements {
  symbolDefinitions: Definitions; // 函数相关的定义,宏、结构体、类型定义、枚举、枚举成员、函数、变量
}

// 构造函数分组接口
export interface ConstructorGroup {
  public: string[];
  protected: string[];
  private: string[];
}